package demo.sciau.votesystem.controller;

import demo.sciau.votesystem.entity.PO.UserPO;
import demo.sciau.votesystem.entity.VO.ResultVO;
import demo.sciau.votesystem.service.UserService;
import demo.sciau.votesystem.util.IDUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author beifengtz
 * @Date Created in 10:24 2018/10/28
 * @Description:
 */
@RequestMapping("/user")
@RestController // @Controller + @ResponseBody
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAllUser")
    public ResultVO getAllUser() {
        return userService.getAllUser();
    }

    @GetMapping("/getUser")
    public ResultVO getUser(@RequestParam("page") int page, // 当前页码
                            @RequestParam("limit") int limit) {  // 每页显示的数据量
        return userService.getUser(page, limit);
    }

    @PostMapping("/login")
    public ResultVO login(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("role") int role,
                          HttpServletRequest request) {
        return userService.login(role, username, password, request);
    }

    @PostMapping("/deleteUserById")
    public ResultVO deleteUserById(@RequestParam("uid") String uid) {
        return userService.deleteUserById(uid);
    }

    @PostMapping("/addUser")
    public ResultVO addUser(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam("realName") String realName,
                            @RequestParam(name = "email", required = false) String email) {
        UserPO userPO = new UserPO(IDUtil.getUUID(), username, password, realName, email);
        return userService.addUser(userPO);
    }

    @GetMapping("/updateUser")
    public ResultVO updateUser(@RequestParam("uid") String uid,
                               @RequestParam("password") String password,
                               @RequestParam("realName") String realName,
                               @RequestParam(name = "email", required = false) String email) {
        UserPO userPO = new UserPO(uid, password, realName, email);
        return userService.updateUser(userPO);
    }

    @GetMapping("/checkUser")
    public ResultVO checkUser(@RequestParam("userName") String userName) {
        return userService.checkUser(userName);
    }
}

